package newWork.controller;


import newWork.constant.ResultConstant;
import newWork.entity.ClientEntity;
import newWork.entity.CommonResult;
import newWork.entity.LayUITable;
import newWork.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RequestMapping("/client")
@RestController
public class ClientController {


	@Autowired
	private IClientService service;

	/**
	 * 查询
	 *
	 * @return
	 */
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public CommonResult select(@RequestBody ClientEntity entity) {
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, service.select(entity));
	}

	/**
	 * 更新
	 *
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CommonResult update(@RequestBody ClientEntity entity) {
		service.update(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 添加
	 *
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public CommonResult add(@RequestBody ClientEntity entity) {
		service.add(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 删除
	 *
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public CommonResult delete(@RequestBody ClientEntity entity) {
		service.delete(entity);
		return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
	}

	/**
	 * 查询列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public LayUITable<ClientEntity> list(Integer page, Integer limit) {
		ClientEntity clientEntity = new ClientEntity();
		Integer newPage = (page - 1) * limit;
		clientEntity.setPageSize(limit);
		clientEntity.setStart(newPage);
		int count = Integer.parseInt(service.likeSelectCount(new ClientEntity()).toString());

		List<ClientEntity> select = service.select(clientEntity);
		return new LayUITable<>(0, "SUCCESS", count, select);
	}

}
